package com.rory.gooday.diary;

import com.rory.gooday.blog.BlogValidator;
import com.rory.gooday.common.R;
import com.rory.gooday.common.model.Blog;
import com.rory.gooday.common.model.Diary;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Page;

@Path("/diary")
public class DiaryController extends Controller {
	
	@Inject
	DiaryService service;
	
	public void index() {
		render("diary.html");
	}

	public void data() {
		Page<Diary> a =  service.paginate(getParaToInt("page", 1), getParaToInt("limit", 10));
		renderJson(R.ok(a));
	}
	
	public void add() {
	}
	
	/**
	 * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
	 * 并要对数据进正确性进行验证，在此仅为了偷懒
	 */
	@Before(BlogValidator.class)
	public void save() {
		getBean(Blog.class).save();
		redirect("/blog");
	}
	
	public void edit() {
		Diary a = service.findById(getParaToInt());
		setAttr("blog", a);
	}
	
	/**
	 * save 与 update 的业务逻辑在实际应用中也应该放在 serivce 之中，
	 * 并要对数据进正确性进行验证，在此仅为了偷懒
	 */
	@Before(BlogValidator.class)
	public void update() {
		getBean(Blog.class).update();
		redirect("/blog");
	}
	
	public void delete() {
		service.deleteById(getParaToInt());
		redirect("/blog");
	}
}


